package antlr4.definitive.d4_expr.d4_3_0_extractinterface.run;

import antlr4.definitive.d4_expr.d4_3_0_extractinterface.gen.JavaLexer;
import antlr4.definitive.d4_expr.d4_3_0_extractinterface.gen.JavaParser;
import cn.hutool.core.io.FileUtil;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

public class Main {

    public static void main(String[] args) {
        if (args.length > 0) {
            String arg = args[0];
            String content = FileUtil.readString(arg, "UTF-8");
            System.out.println("读取文件" + arg + ",内容:\n" + content);
            CodePointCharStream input = CharStreams.fromString(content);
            System.out.println("开始解析文件");

            JavaLexer lexer = new JavaLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            JavaParser parser = new JavaParser(tokens);
            //4.3利用监听器构建一个翻译程序
            ParseTree tree = parser.compilationUnit();//开始语法分析的过程
            ParseTreeWalker walker = new ParseTreeWalker();//新建一个标准的遍历器
            ExtractInterfaceListener extractor = new ExtractInterfaceListener(parser);
            walker.walk(extractor, tree);//使用监听器初始化对语法分析树的遍历

        } else {
            System.err.println("不推荐手写字符串，还是读取一个已经存在的文件吧");
        }

    }

}
